package com.example.gulimall.coupon.service.impl;

import com.example.common.to.MemberPrice;
import com.example.common.to.SkuReductionTo;
import com.example.gulimall.coupon.entity.MemberPriceEntity;
import com.example.gulimall.coupon.entity.SkuLadderEntity;
import com.example.gulimall.coupon.service.MemberPriceService;
import com.example.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.gulimall.coupon.dao.SkuFullReductionDao;
import com.example.gulimall.coupon.entity.SkuFullReductionEntity;
import com.example.gulimall.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {


    @Autowired private SkuLadderService skuLadderService;
    @Autowired private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveReductionInfo(SkuReductionTo skuReductionTo) {
        // sku 优惠、满减信息   gulimall_sms -> sms_sku_ladder、sms_sku_full_reduction、sms_member_price

        // 优惠 sms_sku_ladder
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(skuReductionTo, skuLadderEntity);
        skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());

        if (skuLadderEntity.getFullCount() > 0) {
            this.skuLadderService.save(skuLadderEntity);
        }


        // 满减信息 sms_sku_full_reduction
        SkuFullReductionEntity entity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo, entity);
        if (entity.getFullPrice().compareTo(BigDecimal.ZERO) > 0) {
            this.save(entity);
        }

        // 会员价格 sms_member_price
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        List<MemberPriceEntity> priceEntities = memberPrice.stream().map(
                a ->
                        new MemberPriceEntity()
                                .setSkuId(skuReductionTo.getSkuId())
                                .setMemberLevelId(a.getId())
                                .setMemberLevelName(a.getName())
                                .setMemberPrice(a.getPrice())
                                .setAddOther(1)
        )
                .filter(me -> me.getMemberPrice().compareTo(BigDecimal.ZERO) > 0)
                .collect(Collectors.toList());

        this.memberPriceService.saveBatch(priceEntities);
    }

}